package com.zznode.redsun.commons.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.zznode.csp.domain.commons.query.Page;
import com.zznode.redsun.commons.sercurity.shiro.LoginUser;

/**
 * Action基类，封装公共方法：获取参数、添加参数、分页相关
 * 
 * @author taoping
 * 
 */
@ParentPackage("json-default")
public class AbstractBaseAction extends ActionSupport {

	protected static final String GRID_PAGE_NUMBER = "page";
	protected static final String GRID_PAGE_SIZE = "rows";

	protected static final String JSON_FLAG_ROWS = "rows.*";
	protected static final String JSON_FLAG_MESSAGE = "message";
	/**
	 * 定义JSON中所需包含的对象
	 */
	protected static final String JSON_PARAM_INPROPERTIES = "includeProperties";
	/**
	 * 定义是否忽略父类属性的访问
	 */
	protected static final String JSON_PARAM_IGNOREHIERARCHY = "ignoreHierarchy";

	protected static final String JSON_TYPE = "json";

	private static final long serialVersionUID = 1L;

	private Collection<Object> rows;
	private String message;

	/**
	 * 获取页面列表中的当前页:pageNumber，默认返回0,与Page对象对应
	 * 
	 * @return
	 */
	protected int getPageNumber() {
		if (null != ActionContext.getContext().getParameters()
				&& ActionContext.getContext().getParameters().containsKey(GRID_PAGE_NUMBER)) {
			Object number = ActionContext.getContext().getParameters().get(GRID_PAGE_NUMBER);
			if (null != number) {
				if (ObjectUtils.isArray(number)) {
					return Integer.parseInt((String) ((Object[]) number)[0]);
				} else {
					return Integer.parseInt((String) number);
				}
			}
		}

		return Page.DEFAULT_PAGE_NUMBER;
	}

	/**
	 * 获取页面列表中的每页显示行:pageSize，默认返回50
	 * 
	 * @return
	 */
	protected int getPageSize() {
		if (null != ActionContext.getContext().getParameters()
				&& ActionContext.getContext().getParameters().containsKey(GRID_PAGE_SIZE)) {
			Object pageSize = ActionContext.getContext().getParameters().get(GRID_PAGE_SIZE);
			if (null != pageSize) {
				if (ObjectUtils.isArray(pageSize)) {
					return Integer.parseInt((String) ((Object[]) pageSize)[0]);
				} else {
					return Integer.parseInt((String) pageSize);
				}
			}
		}

		return Page.DEFAULT_DATA_SIZE_IN_ONE_PAGE;
	}

	/**
	 * 添加参数到ValueStack
	 * 
	 * @param key
	 * @param entry
	 */
	protected void putValueStack(String key, Object entry) {
		if (StringUtils.hasText(key)) {
			ActionContext.getContext().getValueStack().set(key, entry);
		}
	}

	/**
	 * ValueStack#setExprOverrides
	 * 
	 * @param overrides
	 */
	protected void putValueStackByExprOverrides(Map<Object, Object> overrides) {
		if (!CollectionUtils.isEmpty(overrides)) {
			ActionContext.getContext().getValueStack().setExprOverrides(overrides);
		}
	}

	/**
	 * ValueStack#getExprOverrides
	 * 
	 * @return
	 */
	protected Map<Object, Object> getValueStackByExprOverrides() {
		return ActionContext.getContext().getValueStack().getExprOverrides();
	}

	/**
	 * 参数获取后转换为相应类型，参数获取顺序：Request, ValueStack, Session, Application
	 * 
	 * @param entryClass
	 * @param key
	 * @return
	 */
	protected <T extends Object> T getValueStack(Class<T> entryClass, String key) {

		Assert.notNull(entryClass, "Generic class parameter is null.");
		Object object = getValueStack(key);

		if (null != object && entryClass.isInstance(object)) {
			return entryClass.cast(object);
		}

		return null;
	}

	/**
	 * 参数获取顺序：Request, ValueStack, Session, Application
	 * 
	 * @param key
	 * @return
	 */
	protected Object getValueStack(String key) {
		if (StringUtils.hasText(key)) {
			ActionContext context = ActionContext.getContext();
			if (context.getParameters().containsKey(key)) {
				return context.getParameters().get(key);
			}
			if (context.getValueStack().getContext().containsKey(key)) {
				return context.getValueStack().getContext().get(key);
			}
			if (context.getSession().containsKey(key)) {
				return context.getSession().get(key);
			}
			if (context.getApplication().containsKey(key)) {
				return context.getApplication().get(key);
			}
		}
		return null;
	}

	/**
	 * 获取Request参数并转换为字符串
	 * 
	 * @param key
	 * @return
	 */
	protected String getParameter(String key) {
		if (StringUtils.hasText(key)) {
			ActionContext context = ActionContext.getContext();
			if (context.getParameters().containsKey(key)) {
				Object param = context.getParameters().get(key);
				if (ObjectUtils.isArray(param)) {
					return (String) ((Object[]) param)[0];
				}
				return (String) param;
			}
		}
		return null;
	}

	/**
	 * 获取当前用户
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	protected LoginUser<Long> getCurrentUser() {
		return (LoginUser<Long>) SecurityUtils.getSubject().getPrincipal();
	}

	/**
	 * 转化为 jeasyui datagrid的JSON集合,属性名：rows
	 * 
	 * @param results
	 */
	protected void fillJsonGridRows(Collection<?> results) {
		if (null == this.rows) {
			this.rows = new ArrayList<Object>();
		} else {
			this.rows.clear();
		}
		if (!CollectionUtils.isEmpty(results)) {
			this.rows.addAll(results);
		}
	}

	/**
	 * Ajax调用时，设置返回信息
	 * 
	 * @param message
	 */
	protected void setJsonMessage(String message) {
		this.message = message;
	}

	public String getMessage() {
		return message;
	}

	public Collection<Object> getRows() {
		return rows;
	}

	public void setRows(Collection<Object> rows) {
		this.rows = rows;
	}

	public void setMessage(String message) {
		this.message = message;
	}
}
